﻿using Business.Base.Common;
using Business.Base.DB;
using Business.Base.DB.DBRules;
using Business.Base.Enums;
using Business.Base.Service.impl;
using Business.CentrifugalPump.Perf;
using Business.NPSH;
using Business.ScrewPump.Perf;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.Base.Service.Create
{

    /// <summary>
    /// 试验服务容器，在系统初始化的时候，加载各类服务容器
    /// </summary>
    public class ServiceContainer
    {
        static ServiceContainer obj = null;
        static object lockObj = new object();
        static Dictionary<ExperimentType, ExperimentService> experimentExcuteServiceContainer ;

        /*
        static ServiceContainer()
        {
            
            
        }

        private ServiceContainer()
        {           
           init();
        }*/

        /// <summary>
        /// 该函数只允许初始化的时候首次调用
        /// </summary>
        public static void init()
        {



            DataSource.init();
            DBMappingRules.init();
            MysqlDBManager.init();
            experimentExcuteServiceContainer = new Dictionary<ExperimentType, ExperimentService>();
            experimentExcuteServiceContainer.Add(ExperimentType.CentrifugalPumpPerf, initCentrifugalPumpPerf());
            experimentExcuteServiceContainer.Add(ExperimentType.ScrewPumpPerf, initScrewPumpPerf());
            experimentExcuteServiceContainer.Add(ExperimentType.NPSH, initNPSH());        
            obj = new ServiceContainer();
        }


        static ExperimentService initCentrifugalPumpPerf()
        {
            ExperimentDBService experimentDBService = new ExperimentDBService();
            experimentDBService.init(typeof(CentrifugalPumpPerfExperiment), typeof(CentrifugalPumpPerfExperimentRecord));
            ICaculateService caculateService = new CentrifugalPumpPerfExperimentCaculateService();
            ExperimentExcelService experimentExcelService = new ExperimentExcelService( new CentrifugalPumpPerfExperimentExcelManager());
            ExperimentService experimentService = new ExperimentService(experimentDBService, caculateService, experimentExcelService);
            return experimentService;
        }

        static ExperimentService initScrewPumpPerf()
        {
            ExperimentDBService experimentDBService = new ExperimentDBService();
            experimentDBService.init(typeof(ScrewPumpPerfExperiment), typeof(ScrewPumpPerfExperimentRecord));
            ICaculateService caculateService = new ScrewPumpPerfExperimentCaculateService();
            ExperimentExcelService experimentExcelService = new ExperimentExcelService( new ScrewPumpPerfExperimentExcelManager());
            ExperimentService experimentService = new ExperimentService(experimentDBService, caculateService, experimentExcelService);
            return experimentService;
        }

        static ExperimentService initNPSH()
        {
            ExperimentDBService experimentDBService = new ExperimentDBService();
            experimentDBService.init(typeof(NpshExperiment), typeof(NpshExperimentRecord));
            ICaculateService caculateService = new NpshExperimentCaculateService();
            ExperimentExcelService experimentExcelService = new ExperimentExcelService(new NpshExperimentExcelManager());
            ExperimentService experimentService = new ExperimentService(experimentDBService, caculateService, experimentExcelService);
            return experimentService;
        }

        

        public static ServiceContainer getInstance()
        {
            return obj;
        }

        
        public ExperimentService this[ExperimentType type]
        {
            get
            {
                return experimentExcuteServiceContainer[type];
            }
        }
        
    }
}
